// pages/tjbx/tjbx.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:"",
    lou:"",
    ss:"",
    img1:"",
    img2:"",
    imgCount:0,//上传图片的数量
    completeCount:0//完成上传的数量
  },
  img:function(e){
    var that=this
    wx.showActionSheet({
      itemList: ['相机','相册'],
      itemColor: 'itemColor',
      success: (result) => {
        switch (result.tapIndex) {
          case 0:
            wx.chooseImage({
              count:1,
              sourceType:['camera'],
              complete: (res) => {
                if(e.currentTarget.id=='i1'){
                  that.setData({img1:res.tempFilePaths})
                }else{
                  that.setData({img2:res.tempFilePaths})
                }
                
              },
            })
            break;
          case 1:
            wx.chooseImage({
              count:1,
              sourceType:['album'],
              complete: (res) => {
                if(e.currentTarget.id=='i1'){
                  that.setData({img1:res.tempFilePaths})
                }else{
                  that.setData({img2:res.tempFilePaths})
                }
              },
            })
            break;
        }
      },
    })
  },
  changeimg:function(e){
    console.log(e)
    var that=this
    wx.showActionSheet({
      itemList: ['重新上传','预览图片','删除'],
      itemColor: 'itemColor',
      success: (result) => {
        switch (result.tapIndex) {
          case 0:
            wx.chooseImage({
              count:1,
              sourceType:['album','camera'],
              complete: (res) => {
                if(e.currentTarget.id=='ci1'){
                  that.setData({img1:res.tempFilePaths})
                }else{
                  that.setData({img2:res.tempFilePaths})
                }
                
              },
            })
            break;
          case 1:
            if(e.currentTarget.id=='ci1'){
              wx.previewImage({
                current: '', // 当前显示图片的http链接
                urls: that.data.img1 // 需要预览的图片http链接列表
              })
            }else{
              wx.previewImage({
                current: '', // 当前显示图片的http链接
                urls: that.data.img2 // 需要预览的图片http链接列表
              })
            }
            break;
          case 2:
            if(e.currentTarget.id=='ci1'){
              that.setData({img1:''})
            }else{
              that.setData({img2:''})
            }
            break;
        }
      },
    })
  },
  formSubmit:function(e){
    var that=this
    var user_id = wx.getStorageSync('userid')
    var password = wx.getStorageSync('password')
    if(e.detail.value.title==""){
      wx.showToast({
        title: '标题不能为空',
        icon:"none"
      })
    }else if(e.detail.value.des==""){
      wx.showToast({
        title: '内容不能为空',
        icon:"none"
      })
    }else{
    wx.request({
      url: app.globalData.URL+'/dormitory_repair/create_repair',
      method:"POST",
      data:{
        user_id,
        password,
        title:e.detail.value.title,
        des:e.detail.value.des
      },
      success:(resp) =>{
        console.log(resp.data)
            wx.showToast({
              title: resp.data.errmsg,
              icon: 'none',
            })

            //判断是否申请成功，如果已经成功，则继续上传图片
            if (resp.data.result=="s") {
              if (that.data.img1!="") {
                //待上传图片数量+1
                that.data.imgCount++;
                //第一个位置图片存在,则上传
                that.uploadImg(that.data.img1[0],resp.data.repair_id);
              }
              if (that.data.img2!="") {
                //待上传图片数量+1
                that.data.imgCount++;
                //第一个位置图片存在,则上传
                that.uploadImg(that.data.img2[0],resp.data.repair_id);
              }
              //如果不需要上传图片，则直接返回
              if (that.data.img1==""&&that.data.img2=="") {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }
            }
      }
    })
  }
  },

  //上传报修图片的逻辑
uploadImg : function(path,repair_id){
  var that = this;
  wx.uploadFile({
    filePath: path,
    name: 'file',
    url:app.globalData.URL+'/dormitory_repair/upload_repair_img',
    formData:{
      "user_id":wx.getStorageSync('userid'),
      "password":wx.getStorageSync('password'),
      name:'file',
      repair_id:repair_id,
    },
    success:(resp)=>{
      //上传成功，完成数量+1
      that.data.completeCount++;
      //判断上传数量是否等于完成数量，如果等于，则全部操作完成
      if (that.data.completeCount == that.data.imgCount) {
        wx.switchTab({
          url: '/pages/index/index',
        })
      }
    },
    fail:(resp)=>{
      //上传失败，完成数量+1
      that.data.completeCount++;
      //判断上传数量是否等于完成数量，如果等于，则全部操作完成
      if (that.data.completeCount == that.data.imgCount) {
        wx.switchTab({
          url: '/pages/index/index',
        })
      }
    }
  })
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      name:wx.getStorageSync('name'),
      lou:wx.getStorageSync('lou'),
      ss:wx.getStorageSync('ss')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})